#!/bin/bash
set -e

# capture all output to a file
# script -e .script_output

TLIBC_DIR="$(dirname "$(readlink -f "$0")")"
THESEUS_BASE_DIR=$TLIBC_DIR/..
THESEUS_CARGO_PATH="$THESEUS_BASE_DIR/tools/theseus_cargo"
THESEUS_DEPS_DIR="$THESEUS_BASE_DIR/build/deps"

## The file name of the compiled output of tlibc's C code components.
TLIBC_C_OUT_FILENAME="libtlibc_c.a"


export RUST_BACKTRACE=1

### Note: the "theseus_cargo" tool must be installed locally instead of invoked via `cargo run` 
cargo install --force --locked --path=$THESEUS_CARGO_PATH --root=$THESEUS_CARGO_PATH

### Do a full clean build every time at this point
cargo clean

### Use theseus_cargo to build this cargo package (tlibc) 
### with an automatic configuration that builds it to depend against pre-built Theseus crates.
$THESEUS_CARGO_PATH/bin/theseus_cargo  --input $THESEUS_DEPS_DIR  build


### The original output files built by cargo are invalid, so we remove them.
rm -vf ./target/x86_64-unknown-theseus/release/libtlibc.a
rm -vf ./target/x86_64-unknown-theseus/release/libtlibc.rlib
rm -vf ./target/x86_64-unknown-theseus/release/libtlibc.d


### tlibc's Cargo.toml now specifies the `staticlib` crate type, so the .a file is generated by rustc.
### However, it exists in the target/.../deps/ file, and it has a hash appended to it.
### We need to copy that file into the expected target folder and remove the hash.
cp -vf ./target/x86_64-unknown-theseus/release/deps/libtlibc-*.a  ./target/x86_64-unknown-theseus/release/libtlibc.a


set -x

### Create a partially-linked object (.o) file from all of the tlibc crate object files,
### being sure to include the C code components.
### Note that this will NOT include ALL Theseus .o dependencies, whereas the .a staticlib does.
ld -r -o  ./target/x86_64-unknown-theseus/release/tlibc.o   \
    ./target/x86_64-unknown-theseus/release/deps/*.o        \
    $(find ./target/x86_64-unknown-theseus/release/build/tlibc* -name "*.o")
    # $(find ./target/x86_64-unknown-theseus/release/ -name $TLIBC_C_OUT_FILENAME)


### Attempt to statically link everything together in a way we can overwrite the relocations later. 
### Note: this should be done only when building the final C application executable binary, not here.
#
# reset
# ld --emit-relocs -o  ./target/x86_64-unknown-theseus/release/tlibc_static  \
#     -u main  \
#     ./target/x86_64-unknown-theseus/release/deps/*.o  \
#     $THESEUS_BASE_DIR/target/x86_64-unknown-theseus/release/libnano_core.a \
#     $THESEUS_BASE_DIR/target/x86_64-unknown-theseus/release/deps/*.o \


    

####################  Old stuff  ########################
### Using `ar` to generate a static library from object files.
### Note: this doesn't really work.
# ar -rcs ./target/x86_64-unknown-theseus/release/libtlibc.a  \
#     ./target/x86_64-unknown-theseus/release/deps/*.o        \
#     $(find ./target/x86_64-unknown-theseus/release/build/tlibc* -name "*.o")


### It also does NOT include the C components, so we need to include those as well.
# ar -rcT ./target/x86_64-unknown-theseus/release/libtlibc_full.a              \
#     ./target/x86_64-unknown-theseus/release/deps/*.a                         \
#     ./target/x86_64-unknown-theseus/release/build/tlibc-*/out/libtlibc_c.a   


# (                                                                        \
#     cd ./target/x86_64-unknown-theseus/release  &&                               \
#     ar -cqT libtlibc_full.a                                              \
#         ./target/x86_64-unknown-theseus/release/./deps/*.a                                                       \
#         ./target/x86_64-unknown-theseus/release/./build/tlibc-*/out/libtlibc_c.a                                 \
#         &&                                                               \
#     ar -M <(echo -e 'create libtlibc_full.a\naddlib libtlibc_full.a\nsave\nend')  \
# )

# ar -rcs ./target/x86_64-unknown-theseus/release/libtlibc_nested.a  \
#     ./target/x86_64-unknown-theseus/release/deps/*.a             \
#     $(find ./target/x86_64-unknown-theseus/release/build/tlibc* -name "*.o")
# ### an alternative to the above
# ar -rcs ./target/x86_64-unknown-theseus/release/libtlibc_nested_alt.a  \
#     ./target/x86_64-unknown-theseus/release/deps/*.a          \
#     ./target/x86_64-unknown-theseus/release/build/tlibc-*/out/libtlibc_c.a
   